package com.jml.callable;


/**
 *
 */

public class Wait和NotifyAll {

    final static Object object = new Object();

    public static class T1 extends Thread {

        public void run() {
            synchronized (object) {
                System.out.println(System.currentTimeMillis() + ":T1 start! ");
                try {
                    System.out.println(System.currentTimeMillis() + ":T1 wait for object ");
                    object.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(System.currentTimeMillis() + ":T1 week!");
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(System.currentTimeMillis() + ":T1 end!");
            }
        }
    }

    public static class T2 extends Thread {

        public void run() {
            synchronized (object) {
                System.out.println(System.currentTimeMillis() + ":T2 start! ");
                try {
                    System.out.println(System.currentTimeMillis() + ":T2 wait for object ");
                    object.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(System.currentTimeMillis() + ":T2 week!");
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(System.currentTimeMillis() + ":T2 end!");
            }
        }
    }

    public static class T3 extends Thread {

        public void run() {
            synchronized (object) {
                System.out.println(System.currentTimeMillis() + ":T3 start! notify one thread");
                object.notifyAll();
                System.out.println(System.currentTimeMillis() + ":T3 end!");
                try {
                    Thread.sleep(2000);
                    System.out.println("T3休眠完畢");
                } catch (InterruptedException e) {
                }
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new T1();
        Thread t2 = new T2();
        Thread t3 = new T3();
        t1.start();
        Thread.sleep(2000);
        t2.start();
        Thread.sleep(2000);
        t3.start();
    }
}